<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\Template;
use App\Models\TemplatesLog;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use App\Models\Category;
use Illuminate\Support\Facades\Storage;
use App\Helper\Build\Build;

class TemplateController extends Controller
{
    protected $name = '模板管理';
    use ModelForm;
    
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description($this->name);


            $content->body($this->grid());
        });
    }
    
    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->name);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('创建');

            $content->body($this->form());
        });
    }
	
	public function logs()
	{
		return TemplatesLog::find(request()->get('id'));
	}
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Template::class, function (Grid $grid) {
            $grid->model()->orderBy('is_default', 'DESC')->orderBy('updated_at', 'DESC');
            
            $grid->filter(function($filter){
                $filter->like('title', '标题');
				$filter->equal('type', '类型')->select(Template::$type);
            });
			
			$grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->column('id', 'ID')->sortable();
            
            $grid->column('title', '名称');
            $grid->column('is_default', '默认')->options()->select(Template::$default);
            
            $grid->column('updated_at', '更新时间');
        });
    }
    
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Template::class, function (Form $form) {
			
            $form->text('title', '名称')->rules('required');
			$form->select('type', '类型')->options(Template::$type);
			$form->radio('is_default', '默认')->options(Template::$default);
            // $form->textarea('content', '模板内容')->rows(30);
            $form->eidtcode('content', '模板内容');
			$form->select('logs_id', '修改记录')->attribute('onchange', 'select_temp_logs(this)')->options(function($id){
				$Template = new Template();
				return $Template->getTempLogs($this->id);
			});
			$form->ignore(['logs_id']);
			$form->saving(function ($form) {
				// if($form->is_default == 1)
				// {
					// Template::where('type', $form->type)->update(['is_default'=>0]);
				// }
			});
			//生产缓存文件
			$form->saved(function ($form) {
				//模板关键词替换
				Storage::disk('view')->put($form->model()->id.Template::$temp_ext, replaceContent($form->model()->content));
				
			});
            
        });
    }
}
